from unittest import TestCase
from unittest.mock import MagicMock, patch

from django_sso.views import login, logout


class ViewsLoginTests(TestCase):
    def setUp(self):
        from django.conf import settings

        if not settings.configured:
            settings.configure(DEBUG=True)  # pragma: no cover

    def test_login_is_authenticated(self):
        mock_request = MagicMock()
        mock_request.GET = {}
        mock_request.is_secure.return_value = True
        mock_request.get_host.return_value = "foo.bar"
        mock_settings = MagicMock()
        mock_settings.CAS_REDIRECT_URL = "/"
        _ = login(request=mock_request, settings=mock_settings)

    def test_login_no_ticket(self):
        mock_request = MagicMock()
        mock_request.GET = {}
        mock_request.is_secure.return_value = True
        mock_request.get_host.return_value = "foo.bar"
        mock_request.user.is_authenticated = False
        mock_settings = MagicMock()
        mock_settings.CAS_REDIRECT_URL = "/"
        mock_settings.CAS_SERVER_URL = "sso.example.com"
        _ = login(request=mock_request, settings=mock_settings)

    def test_login_has_ticket(self):
        mock_request = MagicMock()
        mock_request.GET = {"ticket": "foo"}
        mock_request.is_secure.return_value = True
        mock_request.get_host.return_value = "foo.bar"
        mock_request.user.is_authenticated = False
        mock_settings = MagicMock()
        mock_settings.CAS_REDIRECT_URL = "/"
        mock_settings.CAS_SERVER_URL = "sso.example.com"
        mock_settings.CAS_RETRY_LOGIN = True
        mock_user = None
        with patch("django_sso.business._get_user", return_value=mock_user):
            _ = login(request=mock_request, settings=mock_settings)

    def test_login_user_not_none(self):
        mock_request = MagicMock()
        mock_request.GET = {"ticket": "foo"}
        mock_request.is_secure.return_value = True
        mock_request.get_host.return_value = "foo.bar"
        mock_request.user.is_authenticated = False
        mock_settings = MagicMock()
        mock_settings.CAS_REDIRECT_URL = "/"
        mock_settings.CAS_SERVER_URL = "sso.example.com"
        mock_settings.CAS_RETRY_LOGIN = False
        mock_user = MagicMock()
        with patch("django_sso.business._get_user", return_value=mock_user):
            with patch("django_sso.business._login_user"):
                _ = login(request=mock_request, settings=mock_settings)

    def test_login_user_no_retry(self):
        mock_request = MagicMock()
        mock_request.GET = {"ticket": "foo"}
        mock_request.is_secure.return_value = True
        mock_request.get_host.return_value = "foo.bar"
        mock_request.user.is_authenticated = False
        mock_settings = MagicMock()
        mock_settings.CAS_REDIRECT_URL = "/"
        mock_settings.CAS_SERVER_URL = "sso.example.com"
        mock_settings.CAS_RETRY_LOGIN = False
        mock_user = None
        with patch("django_sso.business._get_user", return_value=mock_user):
            _ = login(request=mock_request, settings=mock_settings)


class ViewsLogoutTests(TestCase):
    def setUp(self):
        from django.conf import settings

        if not settings.configured:
            settings.configure(DEBUG=True)  # pragma: no cover

    def test_logout_logout_completely(self):
        mock_request = MagicMock()
        mock_settings = MagicMock()
        mock_settings.CAS_LOGOUT_COMPLETELY = True
        mock_settings.CAS_SERVER_URL = "sso.example.com"
        with patch("django_sso.business._logout_user"):
            _ = logout(request=mock_request, settings=mock_settings)

    def test_logout(self):
        mock_request = MagicMock()
        mock_request.GET = {}
        mock_request.is_secure.return_value = True
        mock_request.get_host.return_value = "foo.bar"
        mock_settings = MagicMock()
        mock_settings.CAS_LOGOUT_COMPLETELY = False
        mock_settings.CAS_SERVER_URL = "sso.example.com"
        mock_settings.CAS_REDIRECT_URL = "/"
        mock_settings.CAS_IGNORE_REFERER = True
        with patch("django_sso.business._logout_user"):
            _ = logout(request=mock_request, settings=mock_settings)
